/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package nhadat.action;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import nhadat.action.form.GetPDFActionForm;
import nhadat.entity.TblKhuVuc;
import nhadat.entity.TblLoaiGiayTo;
import nhadat.entity.TblLoaiNha;
import nhadat.entity.TblLoaiTin;
import nhadat.entity.TblNguoiDung;
import nhadat.entity.TblTinDang;
import nhadat.xml.XMLmanipulate;
import org.apache.avalon.framework.logger.ConsoleLogger;
import org.apache.avalon.framework.logger.Logger;
import org.apache.fop.apps.Driver;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.xml.sax.InputSource;

/**
 *
 * @author huanht
 */
public class GetPDFAction extends org.apache.struts.action.Action {
    
    /* forward name="success" path="" */
    private static final String SUCCESS = "success";
    
    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

            
        request.setCharacterEncoding("UTF-8");
        
        GetPDFActionForm f = (GetPDFActionForm) form;
        TblTinDang tin = f.xemChiTiet();


        


        TblLoaiNha loainha = new TblLoaiNha();
        TblLoaiGiayTo loaigiayto = new TblLoaiGiayTo();
        TblLoaiTin loaitin = new TblLoaiTin();
        TblNguoiDung nguoidung = new TblNguoiDung();
        TblKhuVuc khuvuc = new TblKhuVuc();

        loainha.setMaloainha(tin.getTblLoaiNha().getMaloainha());
        loainha.setTenloainha(getUnsignedString(tin.getTblLoaiNha().getTenloainha()));

        loaigiayto.setMaloaigiayto(tin.getTblLoaiGiayTo().getMaloaigiayto());
        loaigiayto.setTenloaigiayto(getUnsignedString(tin.getTblLoaiGiayTo().getTenloaigiayto()));

        loaitin.setMaloaitin(tin.getTblLoaiTin().getMaloaitin());
        loaitin.setTenloaitin(getUnsignedString(tin.getTblLoaiTin().getTenloaitin()));

        khuvuc.setMakhuvuc(tin.getTblKhuVuc().getMakhuvuc());
        khuvuc.setQuanhuyen(getUnsignedString(tin.getTblKhuVuc().getQuanhuyen()));
        khuvuc.setTinhthanh(getUnsignedString(tin.getTblKhuVuc().getTinhthanh()));

        nguoidung.setDiachi(getUnsignedString(tin.getTblNguoiDung().getDiachi()));
        nguoidung.setDienthoai(tin.getTblNguoiDung().getDienthoai());
        nguoidung.setDienthoaididong(tin.getTblNguoiDung().getDienthoaididong());
        nguoidung.setHo(tin.getTblNguoiDung().getHo());
        nguoidung.setTen(tin.getTblNguoiDung().getTen());
        nguoidung.setTendangnhap(tin.getTblNguoiDung().getTendangnhap());
        nguoidung.setEmail(tin.getTblNguoiDung().getEmail());

//        tin2.setMatindang(tin.getMatindang());
        tin.setCautruc(getUnsignedString(tin.getCautruc()));
        tin.setDiachi(getUnsignedString(tin.getDiachi()));
        tin.setTblKhuVuc(khuvuc);
        tin.setTblLoaiGiayTo(loaigiayto);
        tin.setTblLoaiNha(loainha);
        tin.setTblLoaiTin(loaitin);
        tin.setTblNguoiDung(nguoidung);
        tin.setThongtinchitiet(getUnsignedString(tin.getThongtinchitiet()));
        tin.setDvdgiatien(getUnsignedString(tin.getDvdgiatien()));
        tin.setTieudetin(getUnsignedString(tin.getTieudetin()));

        XMLmanipulate creater = new XMLmanipulate();
        String path = getServlet().getServletContext().getRealPath("/");
        tin.setHinhanh(path.replace('\\', '/') + tin.getHinhanh()) ;
        creater.createPDFXMLFullfromTblTinDang(tin, path + "xml/pdfXML.xml");

        try {
            String xslPath = path + "xsl/getPDFFO.xsl";
            String xmlPath = path + "xml/pdfXML.xml";
            String foPath = path + "xml/pdfFO.fo";

            
            f.methodTrax(xslPath, xmlPath, foPath);

            File file = new File(foPath);
            FileInputStream input = new FileInputStream(file);

            ByteArrayOutputStream out = new ByteArrayOutputStream();
            response.setContentType("application/pdf");
            Driver driver = new Driver(new InputSource(input), out);


            Logger log = new ConsoleLogger(ConsoleLogger.LEVEL_WARN);
            driver.setRenderer(Driver.RENDER_PDF);
            driver.run();

            byte[] content = out.toByteArray();
            response.setContentLength(content.length);
            response.getOutputStream().write(content);
            response.getOutputStream().flush();




        } catch( Exception e) {
            e.printStackTrace();
        }



        return mapping.findForward(SUCCESS);
    }
     public static String getUnsignedString(String s) {
            StringBuffer unsignedString = new StringBuffer();
            for (int i = 0; i < s.length(); i++) {
                unsignedString.append(getUnsignedChar(s.charAt(i)));
            }
            return unsignedString.toString();
	}
    public static char getUnsignedChar(char c) {
        if (c=='\u00E1'||c=='\u00E0'||c=='\u1EA3'||c=='\u00E3'||c=='\u1EA1'||
            c=='\u0103'||c=='\u1EAF'||c=='\u1EB1'||c=='\u1EB3'||c=='\u1EB5'||c=='\u1EB7'||
            c=='\u00E2'||c=='\u1EA5'||c=='\u1EA7'||c=='\u1EA9'||c=='\u1EAB'||c=='\u1EAD') {
            return 'a';
        } else if (c=='\u00C1'||c=='\u00C0'||c=='\u1EA2'||c=='\u00C3'||c=='\u1EA0'||
            c=='\u0102'||c=='\u1EAE'||c=='\u1EB0'||c=='\u1EB2'||c=='\u1EB4'||c=='\u1EB6'||
            c=='\u00C2'||c=='\u1EA4'||c=='\u1EA6'||c=='\u1EA8'||c=='\u1EAA'||c=='\u1EAC') {
            return 'A';
        } else if (c=='\u00E9'||c=='\u00E8'||c=='\u1EBB'||c=='\u1EBD'||c=='\u1EB9'||
            c=='\u00EA'||c=='\u1EBF'||c=='\u1EC1'||c=='\u1EC3'||c=='\u1EC5'||c=='\u1EC7') {
            return 'e';
        } else if (c=='\u00C9'||c=='\u00C8'||c=='\u1EBA'||c=='\u1EBC'||c=='\u1EB8'||
            c=='\u00CA'||c=='\u1EBE'||c=='\u1EC0'||c=='\u1EC2'||c=='\u1EC4'||c=='\u1EC6') {
            return 'E';
        } else if (c=='\u00ED'||c=='\u00EC'||c=='\u1EC9'||c=='\u0129'||c=='\u1ECB') {
            return 'i';
        } else if (c=='\u00CD'||c=='\u00CC'||c=='\u1EC8'||c=='\u0128'||c=='\u1ECA') {
            return 'I';
        } else if (c=='\u00F3'||c=='\u00F2'||c=='\u1ECF'||c=='\u00F5'|c=='\u1ECD'||
            c=='\u00F4'||c=='\u1ED1'||c=='\u1ED3'||c=='\u1ED5'||c=='\u1ED7'||c=='\u1ED9'||
            c=='\u01A1'||c=='\u1EDB'||c=='\u1EDD'||c=='\u1EDF'||c=='\u1EE1'||c=='\u1EE3') {
            return 'o';
        } else if (c=='\u00D3'||c=='\u00D2'||c=='\u1ECE'||c=='\u00D5'|c=='\u1ECC'||
            c=='\u00D4'||c=='\u1ED0'||c=='\u1ED2'||c=='\u1ED4'||c=='\u1ED6'||c=='\u1ED8'||
            c=='\u01A0'||c=='\u1EDA'||c=='\u1EDC'||c=='\u1EDE'||c=='\u1EE0'||c=='\u1EE2') {
            return 'O';
        } else if (c=='\u00FA'||c=='\u00F9'||c=='\u1EE7'||c=='\u0169'|c=='\u1EE5'||
            c=='\u01B0'||c=='\u1EE9'||c=='\u1EEB'||c=='\u1EED'||c=='\u1EEF'||c=='\u1EF1') {
            return 'u';
        } else if (c=='\u00DA'||c=='\u00D9'||c=='\u1EE6'||c=='\u0168'|c=='\u1EE4'||
            c=='\u01AF'||c=='\u1EE8'||c=='\u1EEA'||c=='\u1EEC'||c=='\u1EEE'||c=='\u1EF0') {
            return 'U';
        } else if (c=='\u00FD'||c=='\u1EF3'||c=='\u1EF7'||c=='\u1EF9'||c=='\u1EF5') {
            return 'y';
        } else if (c=='\u00DD'||c=='\u1EF2'||c=='\u1EF6'||c=='\u1EF8'||c=='\u1EF4') {
            return 'Y';
        } else if(c=='\u0111'){
            return 'd';
        }else if(c=='\u0110'){
            return 'D';
        }
        return c;
    }
}
